import { defineStore } from 'pinia';
import store from 'store2';
import {find,filter} from 'lodash';
const userWebSiteStore = defineStore('webSiteStore', {
    state() {
        return {
            websites: []
        }
    },
    actions: {
        //初始化
        init() {
            const websites = store.get("websites")||[];
            this.websites = websites;
        },
        //添加
        add(item) {
            if (find(this.websites, { url: item.url })) {
                myApi.alert('此网站已经被添加')
            } else {
                this.websites.unshift(item)
                store('websites', this.websites)
            }
        },
        //删除
        delete(item) {
            this.websites = this.websites.filter((value) => {
                return value.url !== item.url
            })
            store("websites", this.websites);
        }
    },
    getters: {
        find() {
            return function(keywords){
                if(keywords==='') return this.websites
                return filter(this.websites,(item)=>{
                    let patten=new RegExp(keywords,'i');
                    return patten.test(item.title)
                })
            }
        }
    }
})
export default userWebSiteStore;